import { _decorator, Component, Node, Vec3 } from 'cc';
import { Bun } from './Bun';
import { GameScene } from '../GameScene';
import { PlateLoad } from './PlateLoad';
const { ccclass, property } = _decorator;

@ccclass('SingleLineBun')
export class SingleLineBun extends Component {

    //盘子层
    public PlateLoad: PlateLoad = null;

    /**
     * 初始化
     */
    public Init(_bunindexarr: number[]) {
        for (let bun of this.node.children) bun.active = false;
        for (let i = 0; i < _bunindexarr.length; i++) {
            this.node.children[i].active = true;
            this.node.children[i].getComponent(Bun).UpdateBun(_bunindexarr[i]);
        }
        this.CheckShowShadow();
    }

    /**
     * 判断我这一排是否有可以消除得
     */
    public CheckClear(_bunindex: number, _pos0: Vec3, _pos1: Vec3): boolean {
        let is_clear: boolean = false;
        for (let bun of this.node.children) {
            if (bun.getComponent(Bun).BunIndex != -1) {
                if (bun.getComponent(Bun).BunIndex == _bunindex && !bun.getComponent(Bun).IamInShadow) {
                    bun.getComponent(Bun).PlayDisappearAnimation(_pos0, _pos1, this.DelayCheck.bind(this));
                    is_clear = true;
                }
            }
        };
        return is_clear;
    }

    /**
     * 第一排消失
     */
    public FirstRowClear() {
        for (let bun of this.node.children) {
            if (bun.getComponent(Bun).BunIndex != -1) {
                if (!bun.getComponent(Bun).IamInShadow) {
                    bun.getComponent(Bun).IamDisappear(this.DelayCheck.bind(this));
                }
            }
        };
    }

    /**
     * 延时判断
     */
    public DelayCheck() {
        this.CheckShowShadow();
        this.PlateLoad.CheckGameOver();
    }

    /**
     * 判断显示阴影
     */
    public CheckShowShadow() {
        for (let i = 0; i < this.node.children.length; i++) {
            if (i == this.node.children.length - 1) {
                this.node.children[i].getComponent(Bun).IamShowShadow(false);
            } else {
                this.node.children[i].getComponent(Bun).IamShowShadow(this.node.children[i + 1].getComponent(Bun).BunIndex != -1);
            }
        }
    }

    /**
     * 判断这一列是否结束了
     */
    public CheckIsEnd(): boolean {
        for (let bun of this.node.children) {
            if (bun.getComponent(Bun).BunIndex != -1) return false;
        }
        return true;
    }

}


